//
//  DownloadViewController.m
//  Pictorial
//
//  Created by SKY on 15/12/29.
//  Copyright © 2015年 sky. All rights reserved.
//

#import "DownloadViewController.h"
#import "DownloadModel.h"
#import "DownloadData.h"

#import "DownloadShowView.h"

#define INTERVAL 15

@interface DownloadViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,DownloadShowViewDelegate>
{
    UIImageView *_nullImage;
}
@property (nonatomic,weak) UICollectionView *collectionView;

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) UIView *bgView;

@end

static NSString *cellID = @"cell";

@implementation DownloadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    [self createUI];
    
    [self createNavBar];
}
-(void)createNavBar{
    UIButton *backBtn = [UIButton buttonWithLocalBgImage:@"close.png" frame:CGRectMake(5, 5, 40, 40) target:self sel:@selector(back)];
    [self.view addSubview:backBtn];
    
    UILabel *titleLabel = [UILabel labelWithText:@"我的下载" fontSize:15 frame:CGRectMake(50, 20, CGWidth(self.bgView.frame) - 50 * 2, 20) color:[UIColor blackColor] textAlignment:1];
    [self.view addSubview:titleLabel];
}
-(void)back{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)createUI{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = INTERVAL;
    layout.minimumLineSpacing = INTERVAL;
    CGFloat w = (CGWidth(self.bgView.frame) - INTERVAL * 3) / 2;
    CGFloat h = w * 1.4;
    layout.itemSize = CGSizeMake(w, h);
    layout.sectionInset = UIEdgeInsetsMake(INTERVAL, INTERVAL, INTERVAL, INTERVAL);
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 50, CGWidth(self.bgView.frame), CGHeight(self.bgView.frame) - 50) collectionViewLayout:layout];
    [self.bgView addSubview:collectionView];
    self.collectionView = collectionView;
    
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
}
#pragma mark - DownloadShowViewDelegate
-(void)alert:(UIAlertController *)alertVC{
    [self presentViewController:alertVC animated:YES completion:nil];
}
-(void)deletePictureAtIndex:(NSInteger)index{
    [DownloadData deletePictureAtIndex:index];
    
    [self.dataArray removeAllObjects];
    self.dataArray = nil;
    
    [self.collectionView reloadData];
}
#pragma mark - CollectionView
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DownloadModel *model = self.dataArray[indexPath.item];
    
    DownloadShowView *showView = [DownloadShowView nibInitializtion];
    [self.view addSubview:showView];
    showView.delegate = self;
    showView.model = model;
    showView.index = indexPath.item;

}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DownloadModel *model = self.dataArray[indexPath.item];
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    UIImageView *imageView = [UIImageView imageViewWithNetImage:model.picture frame:cell.bounds];
    [cell.contentView addSubview:imageView];
    
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
        
        NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:DOWNLOAD_DATA];
        for (int index = 0; index < array.count; index ++) {
            DownloadModel *model = [DownloadData modelFromDataAtIndex:index];
            [_dataArray addObject:model];
        }
        
        if(_dataArray.count == 0){
            _nullImage = [UIImageView imageViewWithLocalImage:@"over.png" frame:CGRectMake(0, 0, 80, 80)];
            [self.bgView addSubview:_nullImage];
            _nullImage.alpha = 0.8;
            _nullImage.center = self.bgView.center;
        }else{
            if (_nullImage != nil) {
                [_nullImage removeFromSuperview];
            }
        }
    }
    return _dataArray;
}
-(UIView *)bgView{
    if (_bgView == nil) {
        _bgView = [UIView classInitializtion];
        [self.view addSubview:_bgView];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.frame = CGRectMake(INTERVAL, INTERVAL, WIDTH - INTERVAL * 2, HEIGHT - INTERVAL * 2);
    }
    return _bgView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
